//
//  OpenBoxView.m
//  MyLittleBrother
//
//  Created by speeda on 17/7/31.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "OpenBoxView.h"
#import <ImageIO/ImageIO.h>
#import "CoverView.h"

@interface OpenBoxView ()

@property (weak, nonatomic) IBOutlet UIImageView *boxImageView;
@property (weak, nonatomic) IBOutlet UILabel *boxNumLabel;
@property (weak, nonatomic) IBOutlet UIButton *openBoxButton;
@property (weak, nonatomic) IBOutlet UILabel *remindLabel;

@property (weak, nonatomic) IBOutlet UILabel *openBoxLabel;

@property (weak, nonatomic) IBOutlet UIView *winView;

@end

@implementation OpenBoxView


+ (void)openBoxViewFormXib {

    [CoverView showCoverView];
    
    OpenBoxView *view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    view.center = [UIApplication sharedApplication].keyWindow.center;
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 0.0;
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    
    [UIView animateWithDuration:0.2 animations:^{
        view.alpha = 1.0;
    }];
}




#pragma mark -- 取消
- (IBAction)cancelBtnAction:(id)sender {
    
    for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        
        if ([view isKindOfClass:[OpenBoxView class]]) {
            
            [view removeFromSuperview];
        }

        if ([view isKindOfClass:[CoverView class]]) {
            
            [view removeFromSuperview];
        }
        
    }
}



#pragma mark -- 点击开盒
- (IBAction)openBoxAction:(id)sender {
    
    self.openBoxButton.hidden = YES;
    self.remindLabel.hidden = YES;
    self.winView.hidden = YES;
    self.openBoxLabel.hidden = NO;
    
//    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"openBox" withExtension:@"gif"]; //加载GIF图片
//    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef) fileUrl, NULL);//将GIF图片转换成对应的图片源
//    size_t frameCout = CGImageSourceGetCount(gifSource);//获取其中图片源个数，即由多少帧图片组成
//    NSMutableArray *frames = [[NSMutableArray alloc] init]; //定义数组存储拆分出来的图片
//    for (size_t i = 0; i < frameCout; i++) {
//        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); //从GIF图片中取出源图片
//        UIImage *imageName = [UIImage imageWithCGImage:imageRef];//将图片源转换成UIimageView能使用的图片源
//        [frames addObject:imageName];  //将图片加入数组中
//        CGImageRelease(imageRef);
//    }

    self.boxImageView.animationImages = @[[UIImage imageNamed:@"box_chose"],[UIImage imageNamed:@"box_open"]]; //将图片数组加入UIImageView动画数组中
    self.boxImageView.animationDuration = 1.0; //每次动画时长
//    self.boxImageView.animationRepeatCount = 5; //动画次数
    [self.boxImageView startAnimating];         //开启动画，此处没有调用播放次数接口，UIImageView默认播放次数为无限次，故这里不做处理
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(transitionAni:) userInfo:nil repeats:NO];
    
}

- (void)transitionAni:(NSTimer *) timer {
    
    [self.boxImageView stopAnimating];
    
    self.winView.hidden = NO;
    
    CATransition * ani = [CATransition animation];
    ani.type = kCATransitionFade;
    ani.subtype = kCATransitionFromBottom;
    ani.duration = 0.5;
    self.boxImageView.image = [UIImage imageNamed:@"img_prize"];
    [self.boxImageView.layer addAnimation:ani forKey:@"transitionAni"];
    
    [timer invalidate];
    timer = nil;
}





@end
